/* --------------------------------------
	Easy2go list entering
-------------------------------------- */
bs_tree enter_list( void ) {
	char t;
	int buf = 0, sgn = 1;
	bs_tree u;
	cout << "Type some values separated by comma:\n";
	fflush( stdin );
	do {
		t = getchar();
		if( t == ' ' || t == '\n' || t == ',' ) {
			u.insert( buf );
			buf = 0;
			sgn = 1;
			continue;
		}
		if( t == '-' ) {
			sgn = -1;
			continue;
		}
		buf = buf*10+sgn*(t-'0');
	} while( t != '\n' );
	#if DEBUG_MODE
		u.debug_list();
		debug("cool.");
	#endif
	return u;
}

namespace tst {
	/* --------------------------------------
	   Internal Memory Test 1
	-------------------------------------- */
	void test_1( void ) {
		bs_tree tr;
		for( int i=0; i<5; i++ )
			tr.insert( rand()%100 );
	}

	/* --------------------------------------
	   The TEST
	-------------------------------------- */
	void test_2_body( int ins = 40 ) {
		bs_tree tr, tr2, tr3;
		for( int i=0; i<ins; i++ ) {
			tr.insert( rand()%512 );
			tr.max();
		}
		
		// delmin x2
		for( int i=0; i<20; i++ ) {
			tr.deletemin();
			tr.insert(rand()%1024);
			tr.find(tr.max());
		}

		for( int i=0; i<20; i++ ) {
			tr.remove( 256+rand()%512 );
		}
		
		// substr 20-tree
		for( int i=0; i<20; i++ )
			tr3.insert( rand()%1024 );
		tr.subst( tr3, tr.count()/2 );

		// excl mini-tree
		for( int i=0; i<20; i++ )
			tr2.insert( rand()%1024 );
		tr2.excl( tr2 );

		//return clock()-start;
	}

	/* --------------------------------------
	   Test cycling, bro
	-------------------------------------- */
	time_t test_2( int t=40 ) {
		time_t start = clock();
			for( int i=0; i<5000; i++ )
					test_2_body( t );
       		return clock()-start;
	}

	/* --------------------------------------
	   Main tester
	-------------------------------------- */
	void main_test( void ) {
		FILE* f = fopen( "dump.txt", "w+" );
		time_t t;

		#if DEBUG_MODE
			std::cout << "\nPlease, DO NOT TEST IN DEBUG MODE!!!";
			std::cout << "\nyou will get a wrong timings.\n";
			_getch();
		#endif

		fprintf( f, "%d\n", (int)X_TESTS/X_SHIFT );
		for( int i=40; i<X_TESTS+40; i+=X_SHIFT ) {
			gotoxy( 5, 5 );
			cout << "\n    Testing... " << (int)(float)(100*(1+(i-40)/X_SHIFT)/(X_TESTS/X_SHIFT)) << "% (test " << ((i-40+1)/X_SHIFT) << " from " << (int)X_TESTS/X_SHIFT << ")";
			cout << "\n   -----------------------------------------------";
			cout << "\n    Data count: " << i << " Time: ";
			t = test_2(i);
			fprintf( f, "%d\t%d\n", i, t );
			cout << t;
		}
		cout << "\n\n        OK. Result saved to \"dump.txt\".";
		fclose(f);
	}
}


/* --------------------------------------
   Some DEV tests (just not used)
-------------------------------------- */
namespace tmp_tst {
	void test195( bs_tree t ) {
		cout << "\n--------------\n";
		cout << "t->lsize = " << t.count() << '\n';
		cout << "t->err =   " << t.err << '\n';
		t.debug_list();
		cout << "\n--------------\n";
		_getch();
	}

	void test196( node t ) {
		cout << "\n--------------\n";
		cout << t.value;
		cout << "\n--------------\n";
		_getch();
	}
}